<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\Tools\ChooseDoctorAction;
use App\Admin\Metrics\NewDevices;
use App\Admin\Repositories\ProjectUser;
use App\Admin\Repositories\User;
use App\Models\Project;
use App\Models\User as ModelsUser;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\Tools;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Row;

class ProjectUserController extends AdminController
{
    protected function title()
    {
        return '项目医师';
    }

    // public function index(Content $content)
    // {
    //     return $content
    //         // ->header('表格')
    //         // ->description('表格功能展示')
    //         ->body(function (Row $row) {
    //             // $row->column(4, new TotalUsers());
    //             // $row->column(4, new NewUsers());
    //             $row->column(4, new NewDevices());
    //         })
    //         ->body($this->grid());
    // }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ProjectUser(), function (Grid $grid) {

            $grid->disableViewButton();
            $grid->disableEditButton();

            $currProjectID = request()->input('project_id', 0);

            $grid->model()->where([
                'project_id' => $currProjectID
            ]);

            $grid->model()->setConstraints([
                'project_id' => $currProjectID
            ]);
            $projectModel = Project::find($currProjectID);

            $grid->column('id')->sortable();
            // $grid->column('project_id');
            // $grid->column('user_id');
            $grid->column('realname', '姓名');
            $grid->column('phone', '手机号');
            $grid->column('unit', '医院名称(单位)');
            $grid->column('unit_level', '医院级别')->using(User::getUnitLevelOptions())->label();
            $grid->column('department', '科室');
            $grid->column('title', '职称')->using(User::getUserTitleOptions())->label();
            $grid->column('created_at')->sortable();
            // $grid->column('updated_at')->sortable();

            $grid->model()->orderBy('id', 'desc');
            /**
             * 工具按钮.
             */
            // 未完结的时候可以选择，已完结无法选择专家
            if ($projectModel->update_info_end == 0) {
                $grid->tools(function (Tools $tools) {

                    if (Admin::user()->can('select-user')) {

                        $tools->append(new ChooseDoctorAction());
                    }
                });
                if (!Admin::user()->can('user-create')) {
                    $grid->disableCreateButton();
                }
            } else {
                $grid->disableDeleteButton();
                $grid->disableCreateButton();
            }

            $grid->actions(function (Grid\Displayers\Actions $actions) use ($currProjectID) {

                // 专家协议
                $actions->prepend('<a href="/agreement?project_id=' . $currProjectID . '&uid=' . $actions->row->user_id . '" class="text-custom" style="margin-right: 10px;"><i class="fa fa-list"></i>专家协议</a>');
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('realname', '专家姓名');
                $filter->equal('phone', '手机号');
                $filter->like('unit', '医院名称(单位)');
                $filter->like('department', '科室');
                $filter->equal('title', '职称')->select(User::getUserTitleOptions());
                $filter->equal('unit_level', '医院级别')->select(User::getUnitLevelOptions());
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ProjectUser(), function (Show $show) {
            $show->field('id');
            $show->field('project_id');
            $show->field('user_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ProjectUser(), function (Form $form) {
            $form->display('id');
            $form->disableListButton();
            // 完成
            $form->tools(function (Form\Tools $tools) {
                $tools->append('<div class="btn-group pull-right" style="margin-right: 5px">
    <a href="javascript:history.back(-1)" class="btn btn-sm btn-primary "><i class="feather icon-list"></i><span class="d-none d-sm-inline">&nbsp;列表</span></a>
</div>');
            });

            $admin = Admin::user();
            $form->hidden('project_id')->value(request()->get('project_id'));
            $form->hidden('company_id')->value($admin->company_id);
            $form->hidden('agent')->value($admin->id);
            $form->hidden('agent_code')->value($admin->employee_number);
            $form->hidden('status')->value(1);
            $form->autocomplete('realname', '真实姓名')->options(ModelsUser::where('status', '1')->pluck('realname', 'id'))->required();

            $form->mobile('phone', '手机号码')->rules('required|regex:/^1[3-9]\d{9}$/', ['regex' => '手机号码格式不正确', 'required' => '请填写手机号码'])->required();
            $form->distpicker([
                'province_id' => '----省----',
                'city_id'     => '----市----',
                'district_id' => '----区----'
            ], '省市');
            $form->text('unit', '医院名称');
            $form->text('department', '科室');
            $form->select('unit_level', '医院级别')->options(User::getUnitLevelOptions())->addElementClass(['unit_level'], false);
            $form->select('title', '职称')->options(User::getUserTitleOptions());

            $form->text('id_number', '身份证号')->rules(['required', 'regex:/^[1-9]\d{5}(18|19|20|21|22)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|[Xx])$/'], ['regex' => '身份证号码格式不正确', 'required' => '请填写身份证号码'])->required()->len(18);

            $form->text('bank_number', '银行卡号')->required();
            $form->text('bank_opening', '开户行')->required()->help('请填写银行全称');
            $form->text('bank_account_open_local', '开户地')->required()->help('请填写省市地区');


            $form->image('certificate1', '职业医师资格证/工作证')->saveFullUrl()->move('tuofu/user-auth/certificate1/' . date('Y/m'))->autoUpload()->saving(function ($path) {
                return httpToHttps($path);
            });
            $form->image('certificate2', '职业医师资格证/工作证')->saveFullUrl()->move('tuofu/user-auth/certificate2/' . date('Y/m'))->autoUpload()->saving(function ($path) {
                return httpToHttps($path);
            });
        });
    }
}
